import 'package:flutter/material.dart';
import 'package:flowerny/model/messonDetail.dart';
import 'package:dio/dio.dart';
import 'package:flowerny/base/config/conf_web.dart';

class MessionDetailPage extends StatefulWidget {
  int _id;
  String _mession_name;
  MessionDetailPage(int id, String mession_name) {
    _id = id;
    _mession_name = mession_name;
  }

  @override
  State createState() {
    return DetailState(_id, _mession_name);
  }
}

class DetailState extends State<MessionDetailPage> {
  int _messionId;
  String _messionName;
  Size screenSize;
  DetailState(int id, String mession_name) {
    _messionId = id;
    _messionName = mession_name;
  }
  Future<MessionDetail> _fetchMessionList() async {
    MessionDetail _messionDetail = null;
    try {
      final response = await Dio().get('${Config.BASE_URL}/mession/get_mession',
          queryParameters: {"id": _messionId});
      if (response.statusCode == 200) {
        _messionDetail = MessionDetail.fromJson(response.toString());
        return _messionDetail;
      }
    } catch (e) {
      print('MessionPage->' + e.toString());
    }
    return _messionDetail;
  }

  @override
  Widget build(BuildContext context) {
    screenSize = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        centerTitle: true,
        brightness: Brightness.dark,
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
        title: Text(
          _messionName,
          style: TextStyle(
            fontSize: 20.0,
            color: Colors.white,
          ),
        ),
      ),
      body: loadDetail(),
    );
  }

  Widget loadDetail() {
    return Container(
      child: FutureBuilder(
        future: _fetchMessionList(),
        builder: (context, AsyncSnapshot snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.waiting:
              return Center(
                child: CircularProgressIndicator(),
              );
            default:
              if (snapshot.hasError) {
                return Text('Error:${snapshot.error}');
              } else {
                return _createView(context, snapshot);
              }
          }
        },
      ),
    );
  }

  Widget _createView(BuildContext context, AsyncSnapshot snapshot) {
    MessionDetail _messionDetail = snapshot.data;
    return SingleChildScrollView(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  padding: const EdgeInsets.only(
                      left: 5.0, top: 5.0, right: 10.0, bottom: 8.0),
                  child: CircleAvatar(
                    backgroundImage: NetworkImage(_messionDetail.m_icon),
                    radius: 24.0,
                    backgroundColor: Colors.white,
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Row(children: <Widget>[
                          Text(
                            _messionDetail.m_name,
                            style: new TextStyle(
                                color: Colors.black, fontSize: 18),
                          ),
                          Container(
                            decoration: new BoxDecoration(
                              color: new Color(0xFFF6F6F8),
                              borderRadius: new BorderRadius.all(
                                  new Radius.circular(6.0)),
                            ),
                            padding: const EdgeInsets.only(
                                top: 3.0, bottom: 3.0, left: 8.0, right: 8.0),
                            margin: const EdgeInsets.only(left: 15.0),
                            child: Text(
                              _messionDetail.m_type_name,
                              style: new TextStyle(
                                  color: Color.fromARGB(255, 0, 215, 198),
                                  fontSize: 13),
                            ),
                          ),
                        ]),
                        Text(
                          _messionDetail.m_slogen,
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                          style: new TextStyle(
                            color: Colors.grey[400],
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ),
                )
              ],
            ),
            Row(
                children: <Widget>[
                  const SizedBox(width: 5),
                  Text(
                    '积分(1000可提现)：',
                    style: new TextStyle(
                        color: Colors.grey,
                        fontSize: 12),
                  ),
                  const SizedBox(width: 5),
                  Text(
                    '${_messionDetail.m_point}',
                    style: new TextStyle(
                        color: Color.fromARGB(255, 0, 215, 198),
                        fontWeight: FontWeight.bold,
                        fontSize: 18),
                  ),
                ]),
            Container(
              padding: const EdgeInsets.only(
                  left: 5.0, top: 5.0, right: 5.0, bottom: 5.0),
              child: Text(
                _messionDetail.m_detail.trim(),
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 14.0,
                ),
              ),
            ),
            Container(
              padding: const EdgeInsets.only(
                  left: 5.0, top: 5.0, right: 5.0, bottom: 5.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text(
                    '任务平台 ： ',
                    style: TextStyle(
                      color: Colors.black54,
                      fontSize: 16,
                    ),
                  ),
                  Text(
                    _messionDetail.m_app_mall,
                    style: TextStyle(
                      color: Colors.black87,
                      fontSize: 16,
                    ),
                  ),
                  Container(
                    padding: const EdgeInsets.only(left: 10.0),
                    child: Image.network(
                      _messionDetail.m_mall_icon,
                      height: 60.0,
                    ),
                  )
                ],
              ),
            ),
            Container(
              alignment: Alignment(0, -1),
              padding: const EdgeInsets.only(
                  left: 5.0, top: 8.0, right: 5.0, bottom: 2.0),
              child: MaterialButton(
                minWidth: screenSize.width*0.9,
                onPressed: () {
                  action(_messionDetail.m_type, _messionDetail.m_out_url);
                },
                color: Color.fromARGB(255, 0, 215, 198),
                child: Text(
                  _messionDetail.m_type == 1 ? '去下载' : '查看',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
              ),
            ),
            const SizedBox(height: 15),
          ],
        ));
  }

  void action(int type, String out_url) {}
}
